package com.xusheng.cli.controller.open;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.base.Attachment;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.enums.FileDirectoryEnum;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.actressModule.Actress;
import com.xusheng.cli.service.ActressService;
import com.xusheng.cli.tools.FileUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 女优(Actress)表控制层
 *
 * @author whr
 * @since 2023-12-26 09:40:43
 */
@Validated
@RestController
@RequestMapping("/open/v1/actress")
public class ActressController {
    /**
     * 服务对象
     */
    @Resource
    private ActressService actressService;

    /**
     * 查询所有数据
     *
     * @param actress 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-12-26 09:40:43
     */
    @GetMapping("queryAll")
    public Result<List<Actress>> queryAll(Actress actress) {
        return Result.success(actressService.list(new QueryWrapper<>(actress)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param actress 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-12-26 09:40:43
     */
    @GetMapping("queryByPage")
    public Result<MyPage<Actress>> queryByPage(MyPage<Actress> page, Actress actress) {
        return Result.success(actressService.page(page, new QueryWrapper<>(actress)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author whr
     * @since 2023-12-26 09:40:43
     */
    @GetMapping("queryOne/{id}")
    public Result<Actress> selectOne(@PathVariable Serializable id) {
        return Result.success(actressService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param actress 实体对象
     * @return 新增结果
     * @author whr
     * @since 2023-12-26 09:40:43
     */
    @PostMapping("insert")
    public Result<Object> insert(@Validated(Save.class) Actress actress, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            Attachment attachment = FileUtils.uploadSingleFile(file, FileDirectoryEnum.AVATAR);
            actress.setAvatarUrl(attachment.getUrl());
            actress.setAvatarPath(attachment.getPath());
        }
        return Result.isSuccess(actressService.save(actress));
    }

    /**
     * 修改数据
     *
     * @param actress 实体对象
     * @return 修改结果
     * @author whr
     * @since 2023-12-26 09:40:43
     */
    @PutMapping("update")
    public Result<Object> update(@Validated(Update.class) Actress actress, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            Attachment attachment = FileUtils.uploadSingleFile(file, FileDirectoryEnum.AVATAR);
            actress.setAvatarUrl(attachment.getUrl());
            actress.setAvatarPath(attachment.getPath());
        }
        return Result.isSuccess(actressService.updateById(actress));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author whr
     * @since 2023-12-26 09:40:43
     */
    @DeleteMapping("delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        Actress actress = new Actress();
        actress.setId(id);
        return Result.isSuccess(actressService.removeById(actress));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author whr
     * @since 2023-12-26 09:40:43
     */
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(actressService.batchDelete(idList, new Actress()));
    }
}

